package com.qmq.app.controller;

import com.qmq.app.annotation.Login;
import com.qmq.common.utils.PhotoUtils;
import com.qmq.common.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app")
public class FileUploadController {

    @Resource
    private PhotoUtils photoUtils;

    @Value("${file.image}")
    private String url;

    @Login
    @PostMapping("/upload")
    public R upload(HttpServletRequest request) {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multiRequest.getFile("myFile");
        String filePath = photoUtils.saveFile(file);
        return R.ok(url.concat(filePath));
    }
}
